package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author qzh
 * @create 2020-02-13 2020/2/13
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;



    //1.添加权限信息
    public void add(Integer[] checkitemIds, Integer role_id) {
        //获取权限信息的id
        if(checkitemIds!=null&&checkitemIds.length>0){
            for (Integer permission_id : checkitemIds) {
                roleDao.setPR(role_id,permission_id);
            }
        }

    }

    //2.查询所有角色信息
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //3.分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        //2.获取每页显示的记录数
        Integer pageSize = queryPageBean.getPageSize();
        //3.获取查询条件
        String queryString = queryPageBean.getQueryString();
        //4.基于mybatis，使用分页插件助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        PageResult pageResult = new PageResult(total,rows);
        return pageResult;
    }

    // 根据id查询勾选了那些角色
    public List<Integer> findByRoleId(Integer id) {
        return roleDao.findByRoleId(id);
    }
}
